import * as prodApi from '@/api/product/index';
import * as api from '@/api/packing/index';
import * as baseApi from '@/api/common/index';
export default {
  data() {
    return {
      page: null,
      loading: false,
      applyId: null,
      baseUrl: process.env.BASE_FILE_URL,
      pk: {},
      options: {
        boxCategoriesOptions: [{
          id: 1,
          labelZhCh: '彩盒',
          name: 'colorBox'
        },
        {
          id: 2,
          labelZhCh: '外箱',
          name: 'outerBox'
        },
        {
          id: 3,
          labelZhCh: '纸套',
          name: 'paperSleeve'
        },
        {
          id: 4,
          labelZhCh: '展示盒',
          name: 'displayBox'
        },
        {
          id: 0,
          labelZhCh: '其他',
          name: 'otherBox'
        }
        ],
        boxFormingMethodOptions: [{
          id: 1,
          labelZhCh: '自成型'
        },
        {
          id: 2,
          labelZhCh: '钉接'
        },
        {
          id: 3,
          labelZhCh: '粘接'
        },
        {
          id: 4,
          labelZhCh: '胶带粘接'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        boxMaterialOptions: [{
          id: 1,
          labelZhCh: 'YAT标准材质'
        },
        {
          id: 2,
          labelZhCh: '客户标准材质'
        }
        ],
        boxSealingMethodOptions: [{
          id: 1,
          labelZhCh: '胶带'
        },
        {
          id: 2,
          labelZhCh: '热熔胶'
        },
        {
          id: 3,
          labelZhCh: '透明标贴'
        },
        {
          id: 4,
          labelZhCh: '机用打包带'
        }
        ],
        liningCategoriesOptions: [{
          id: 1,
          labelZhCh: '瓦楞纸隔板'
        },
        {
          id: 2,
          labelZhCh: '蜂窝纸隔板'
        },
        {
          id: 3,
          labelZhCh: '纸浆模'
        },
        {
          id: 4,
          labelZhCh: '泡沫(EPS)'
        },
        {
          id: 5,
          labelZhCh: '珍珠棉(EPE)'
        },
        {
          id: 6,
          labelZhCh: '塑料托(PP/PVC/PET)'
        }
        ],
        bubbleCategoriesOptions: [{
          id: 1,
          labelZhCh: '上泡壳+彩卡'
        },
        {
          id: 2,
          labelZhCh: '上下泡壳'
        },
        {
          id: 3,
          labelZhCh: '彩卡'
        },
        {
          id: 4,
          labelZhCh: '对折泡壳'
        },
        {
          id: 5,
          labelZhCh: '三折泡壳'
        }
        ],
        bubblePunchOptions: [{
          id: 1,
          labelZhCh: 'YAT蝴蝶孔'
        },
        {
          id: 2,
          labelZhCh: '客户标准蝴蝶孔'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        bubbleSealingMethodOptions: [{
          id: 1,
          labelZhCh: '热封'
        },
        {
          id: 2,
          labelZhCh: '钉封'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        peCategoriesOptions: [{
          id: 1,
          labelZhCh: '有吊卡'
        },
        {
          id: 2,
          labelZhCh: '无吊卡'
        }
        ],
        peThicknessOptions: [{
          id: 1,
          labelZhCh: 'YAT标准'
        },
        {
          id: 2,
          labelZhCh: '客户标准'
        }
        ],
        loadingCategoriesOptions: [{
          id: 1,
          labelZhCh: '单彩盒'
        },
        {
          id: 2,
          labelZhCh: '外箱'
        },
        {
          id: 3,
          labelZhCh: '托盘'
        },
        {
          id: 4,
          labelZhCh: '拼柜'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        trayCategoriesOptions: [{
          id: 1,
          labelZhCh: '瓦楞纸滑托'
        },
        {
          id: 2,
          labelZhCh: '牛皮纸滑托'
        },
        {
          id: 3,
          labelZhCh: '蜂窝纸托盘'
        },
        {
          id: 4,
          labelZhCh: '胶合板托盘'
        },
        {
          id: 5,
          labelZhCh: '熏蒸实木托盘'
        },
        {
          id: 6,
          labelZhCh: '胶合板托盘'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        trayPackingMethodOptions: [{
          id: 1,
          labelZhCh: '打包带'
        },
        {
          id: 2,
          labelZhCh: '打包扣'
        },
        {
          id: 3,
          labelZhCh: '打包膜'
        },
        {
          id: 4,
          labelZhCh: '托盘盖上盖'
        },
        {
          id: 5,
          labelZhCh: '托盘盖下托'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        traySizeOptions: [{
          id: 1,
          labelZhCh: 'YAT标准'
        },
        {
          id: 2,
          labelZhCh: '指定尺寸'
        },
        {
          id: 0,
          labelZhCh: '其他'
        }
        ],
        containerSizeOptions: [{
          id: 1,
          labelZhCh: '20\'',
          name: 'containerTwentyNumber'
        },
        {
          id: 2,
          labelZhCh: '40\'',
          name: 'containerFortyNumber'
        },
        {
          id: 3,
          labelZhCh: '40H',
          name: 'containerFortyhNumber'
        },
        {
          id: 4,
          labelZhCh: '45',
          name: 'containerFortyFiveNumber'
        },
        {
          id: 5,
          labelZhCh: '20冻柜',
          name: 'containerTwentyFreezerNumber'
        },
        {
          id: 6,
          labelZhCh: '40冻柜',
          name: 'containerFortyFreezerNumber'
        },
        {
          id: 0,
          labelZhCh: '其他',
          name: 'containerOtherNumber'
        }
        ],
        logisticsCategoriesOptions: [{
          id: 1,
          labelZhCh: '集装箱海运'
        },
        {
          id: 2,
          labelZhCh: '集装箱空运'
        },
        {
          id: 3,
          labelZhCh: '国内快递'
        },
        {
          id: 4,
          labelZhCh: '国际快递'
        },
        {
          id: 5,
          labelZhCh: '散货集装'
        },
        {
          id: 6,
          labelZhCh: '内销物流'
        }
        ],
        dropTestStandardOptions: [{
          id: 1,
          labelZhCh: '1A'
        },
        {
          id: 2,
          labelZhCh: '2A'
        },
        {
          id: 3,
          labelZhCh: '3A'
        },
        {
          id: 4,
          labelZhCh: '客户标准'
        },
        {
          id: 5,
          labelZhCh: 'Yat标准'
        },
        {
          id: 6,
          labelZhCh: '其他'
        }
        ]
      },
      structureDocsOptions: [{
        fileType: 1,
        labelZhCh: '材质客户',
        file: {}
      },
      {
        fileType: 2,
        labelZhCh: '客户跌落',
        file: {}
      },
      {
        fileType: 3,
        labelZhCh: '其他附件',
        file: {}
      }
      ],
      basicInfo: [{
        label: '编号',
        unit: '',
        element: 'input',
        name: 'applyNo',
        type: '',
        isRequired: true
      },
      {
        label: '申请人',
        unit: '',
        element: 'input',
        name: 'applyUserName',
        type: '',
        hidden: true,
        options: [],
        isRequired: false
      },
      {
        label: '申请部门',
        unit: '',
        element: 'input',
        options: [],
        name: 'departName',
        hidden: true,
        type: '',
        isRequired: false
      },
      {
        label: '标题',
        unit: '',
        element: 'input',
        name: 'title',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '客户',
        unit: '',
        element: 'selectFilter',
        options: [],
        name: 'customerId',
        type: 'filter',
        isRequired: true
      },
      {
        label: '产品小类',
        unit: '',
        element: 'cascader',
        type: '',
        name: 'pdsConfCategoryArray',
        options: [],
        props: {
          value: 'id',
          label: this.language == 'en' ? 'nameEn' : 'nameCn'
        },
        isRequired: true,
        rule: {
          required: true,
          type: 'Array'
        }
      },
      {
        label: '机型号',
        unit: '',
        element: 'selectFilter',
        name: 'plmModelNo',
        type: '',
        isRequired: true
      },
      {
        label: '要求完成日期',
        unit: '',
        element: 'datePicker',
        name: 'requestCompletionDate',
        type: '',
        isRequired: true
      },
      {
        label: '打样数量',
        unit: '',
        element: 'input',
        name: 'proofingQuantity',
        type: 'number',
        isRequired: true
      },
      {
        label: '包装工程师',
        unit: '',
        element: 'departAdmin',
        options: [],
        name: 'packagingStructureEngineer',
        type: '',
        multiple: false,
        isRequired: false
      },
      {
        label: '包装方式',
        unit: '',
        element: 'input',
        name: 'packingMethod',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '规格描述',
        unit: '',
        element: 'input',
        name: 'mainPartDesc',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '本体数量',
        unit: '',
        element: 'input',
        name: 'mainPartQuantity',
        type: 'number',
        col: 100,
        isRequired: true
      },
      {
        label: '配件描述',
        unit: '',
        element: 'input',
        name: 'accessoryDesc',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '配件数量',
        unit: '',
        element: 'input',
        name: 'accessoryQuantity',
        type: 'number',
        col: 100,
        isRequired: true
      },
      {
        label: '备注',
        unit: '',
        element: 'textarea',
        name: 'remark',
        type: '',
        col: 100,
        isRequired: false
      }
      ],
      // 包装要求和物流要求验证规则
      pkDetailRules: {
        boxCategories: {
          required: true
        },
        boxFormingMethod: {
          required: true
        },
        boxMaterial: {
          required: true
        },
        boxSealingMethod: {
          required: true
        },
        liningCategories: {
          required: true
        },
        liningLength: {
          required: true
        },
        liningWidth: {
          required: true
        },
        liningHeight: {
          required: true
        },
        bubblePunch: {
          required: true
        },
        bubbleCategories: {
          required: true
        },
        bubbleSealingMethod: {
          required: true
        },
        pePunch: {
          required: true
        },
        peCategories: {
          required: true
        },
        peSealingMethod: {
          required: true
        },
        peThickness: {
          required: true
        },
        loadingCategories: {
          required: true
        },
        trayCategories: {
          required: true
        },
        trayPackingMethod: {
          required: true
        },
        traySize: {
          required: true
        },
        containerSize: {
          required: true
        },
        logisticsCategories: {
          required: true
        },
        dropTestStandard: {
          required: true
        }
      },
      // 包装要求下拉显示
      activeName: ['1', '2', '3'],
      // 包装要求下拉显示
      activeNameLog: ['1', '2', '3']
    };
  },
  created() {
    this.pk = this.dataInit();
  },
  methods: {
    dataInit() {
      const data = {
        base: {
          pdsConfCategoryArray: []
        },
        // 包装物流详情 -pdsPackingDesignStructureDetail
        detail: {
          boxCategories: []
        }
      } // 基本信息 -pdsPackingApplyStructure
      return JSON.parse(JSON.stringify(data))
    },
    clearValidate() {
      setTimeout(() => {
        if (this.$refs.basicForm) {
          this.$refs.basicForm.$refs.ruleForm.clearValidate();
        }
        if (this.$refs.pkDetailForm) {
          this.$refs.pkDetailForm.clearValidate();
        }
      })
    },
    refreshCb(res) {
      this.loading = false
      if (this.$route.query.type === 'export' && !this.modelPbsId) {
        this.exportLoadingSub = false
        this.setPdfPage()
      }
      if (res.code === 200) {
        res.data.pdsPackingApplyStructure = res.data.pdsPackingApplyStructure || {}
        res.data.pdsPackingApplyStructureDocs = res.data.pdsPackingApplyStructureDocs || []
        if (res.data.pdsPackingApplyStructure.packagingStructureEngineer) {
          this.getDeptPersonsList(
            res.data.pdsPackingApplyStructure.packagingStructureEngineer
          );
        }
        // 客户回显处理
        if (res.data.pdsPackingApplyStructure.customerId &&
          this.page === 'apply'
        ) {
          console.log('this.$refs.basicForm', this.$refs.basicForm);
          // debugger
          if (this.$refs.basicForm && this.$refs.basicForm.getPdsCustomerByName) {
            this.$refs.basicForm.getPdsCustomerByName(
              res.data.pdsPackingApplyStructure.customerName
            );
          }
        }
        const setPackingData = () => {
          res.data.base = res.data.pdsPackingApplyStructure;
          res.data.detail = res.data.pdsPackingDesignStructureDetail || {};
          res.data.designFileList = res.data.designFileList || []
          res.data.detail.boxCategories = res.data.detail.boxCategories ?
            res.data.detail.boxCategories.split(',') :
            [];
          // 初始化是否由包装设计师填写
          const setFlagChecked = () => {
            for (const i in res.data.detail) {
              if (i.indexOf('Flag') > -1) {
                res.data.detail[i] = res.data.detail[i] === 1;
                if (res.data.detail[i]) {
                  this.flagChange(i);
                }
              }
            }
          };
          // 初始化文件
          const setFile = () => {
            this.structureDocsOptions.map((item, index) => {
              item.file = res.data.pdsPackingApplyStructureDocs[index] || {};
            });
          };
          // 初始化需填写装箱数量
          const setContainerNumberRule = () => {
            const containerSize = res.data.detail.containerSize;
            this.options.containerSizeOptions.map(item => {
              if (containerSize == item.id) {
                this.pkDetailRules[item.name] = {};
                this.pkDetailRules[item.name].required = true;
              }
            });
          };
          setFlagChecked();
          setFile();
          setContainerNumberRule();
          this.$set(this, 'pk', res.data);
        };
        setPackingData();
      }
    },
    getApplyStructureDetai() {
      api.getApplyStructureDetai({
        applyId: this.applyId
      }).then(res => {
        this.refreshCb(res);
      });
    },
    /* 获取包装工程师列表 */
    getDeptPersonsList(id) {
      baseApi.getUserByIds(id).then(res => {
        if (res.code == 200) {
          this.basicInfo[9].options = res.data;
        }
      });
    },
    // 产品小类获取
    getPdsConfCategoryTree() {
      prodApi.getPdsConfCategoryTree().then(res => {
        this.basicInfo[5].options = res.data;
      });
    },
    // 由包装工程师选择
    flagChange(type) {
      type = type.slice(0, type.length - 4);
      const detail = this.pk.detail;
      detail[type] = type === 'boxCategories' ? [] : null;
      detail[type + 'Desc'] = null;
      this.pkDetailRules[type].required = !this.pkDetailRules[type].required;
      const resetUnitData = name => {
        detail[name + 'Categories'] = null;
        detail[name + 'Punch'] = null;
        detail[name + 'SealingMethod'] = null;
        detail[name + 'PunchDesc'] = null;
        detail[name + 'CategoriesDesc'] = null;
        detail[name + 'SealingMethodDesc'] = null;
        this.pkDetailRules[name + 'Punch'].required = !this.pkDetailRules[
          name + 'Punch'
        ].required;
        this.pkDetailRules[name + 'SealingMethod'].required = !this
          .pkDetailRules[name + 'SealingMethod'].required;
      };
      if (type === 'bubbleCategories') {
        resetUnitData('bubble');
      }
      if (type === 'peCategories') {
        resetUnitData('pe');
        detail.peThickness = null;
        this.pkDetailRules.peThickness.required = !this.pkDetailRules
          .peThickness.required;
      }
      this.clearValidate();
    }
  }
}
